import { routes } from "@/routes";
import { getMenuData } from "@ant-design/pro-components";
import { useInfo } from "@/hooks/useInfo";
import type { IRoute } from "@/routes";
import { isBoolean } from "lodash-es";

export const clapMenus = (routes: IRoute[]): { layoutRoutes: IRoute[], commonRoutes: IRoute[] } => {
  const layoutRoutes: IRoute[] = [];
  const commonRoutes: IRoute[] = [];
  for (let index = 0; index < routes.length; index++) {
    const item = routes[index];
    if (item) {
      if (item.layout) {
        if (item.children) {
          layoutRoutes.push(...item.children)
        } else {
          layoutRoutes.push(item)
        }
      } else {
        if (item.children) {
          commonRoutes.push(...item.children)
        } else {
          commonRoutes.push(item)
        }
      }
    }
  }
  return { layoutRoutes, commonRoutes };
}

export const filterRoutes = (routes: IRoute[], filterCondition: Function): IRoute[] => {
  const newRoutes = routes.map((route: IRoute) => {
    if (filterCondition(route)) {
      if (route.children) {
        return { ...route, children: filterRoutes(route.children, filterCondition) };
      } else {
        return route
      }
    } else {
      return null;
    }
  }).filter(route => route !== null);
  return newRoutes as IRoute[];
};

export const useRoutes = () => {
  const { userInfo, userMenus } = useInfo();
  const { layoutRoutes, commonRoutes } = clapMenus(routes);
  const isLogin = userInfo.accessToken && userInfo.uid;
  console.log(layoutRoutes)
  console.log(commonRoutes)
  console.log(userInfo)
  console.log(userMenus)
  console.log(isLogin)
  console.log("----------")

  // 已登录
  if (isLogin) {

    return { routes: routes }
  } else {
    return { routes: commonRoutes }
  }
};

export default useRoutes;
